import { _decorator, Component, Node } from 'cc';
import { pbCoder } from '../../script/common/socket/PbProtoCoder';
import { PlayerProto } from './PROTO';
import WebSock from '../../sgame/Socket/WebSock';
import { CS_LOGIN, SC_LOGIN } from '../../proto/proto';
const { ccclass, property } = _decorator;

@ccclass('SfSocketTest')
export class SfSocketTest extends Component {
    private _ws: WebSock;
    start() {
        this._ws = new WebSock("ws://127.0.0.1:9001", pbCoder);
        let loginData: CS_LOGIN = {
            token: "b#MD}#^5KFkp%*!{"
        };

        this._ws.onConnect = () => {
            this._ws.send(PlayerProto.LOGIN, loginData);
            this._ws.on(PlayerProto.LOGIN, (data: SC_LOGIN) => {
                console.log("登录成功", data);
            });

            this._ws.enableHeartBeat(PlayerProto.HEART_BEAT);
            this._ws.on(PlayerProto.HEART_BEAT, () => {
                console.log("收到心跳包");
            })
        }

        this._ws.connect();
    }
}
